package com.sunwhite.herostore.common;

import com.sunwhite.herostore.entity.User;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class UserManager {
    /**
     * 方法不能继承和私有化
     */
    private UserManager() {
    }

    /**
     * 用户字典
     */
    private static final Map<Integer, User> _userMap = new ConcurrentHashMap<>();

    /**
     * 向userMap中放入user
     * @param user
     */
    public static void addUser(User user){
        if (null!=user){
            _userMap.put(user.userId,user);
        }
    }

    public static void remove(Integer userId) {
         _userMap.remove(userId);
    }

    /**
     * 获取map中的所有user
     * @return
     */
    public static Collection<User> listUser() {
        return _userMap.values();
    }

    public static User getUserById(Integer userId){
        if (null==userId){
            return null;
        }
        return _userMap.get(userId);
    }

}
